package com.scm.web.controller.supplier;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.utils.StringUtils;
import com.scm.common.utils.TOTransferHelper;
import com.scm.dto.req.supplier.ScmSupplierProjectReq;
import com.scm.dto.req.supplier.ScmSupplierReq;
import com.scm.dto.resp.product.ScmProductDo;
import com.scm.dto.resp.supplier.SupplierProjectResp;
import com.scm.persist.model.ScmSupplier;
import com.scm.persist.model.purchase.ScmPurchaseInventoryDetail;
import com.scm.service.IScmSupplierService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.scm.common.annotation.Log;
import com.scm.common.enums.BusinessType;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.common.core.page.TableDataInfo;

/**
 * 订单商品Controller
 *
 * @author reptilemanager
 * @date 2024-04-20
 */
@Controller
@RequestMapping("/system/supplier")
public class ScmSupplierController extends BaseController
{
    private String prefix = "supplier";

    @Autowired
    private IScmSupplierService scmSupplierService;

    @RequiresPermissions("system:supplier:view")
    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }

    /**
     * 查询订单商品列表
     */
    @RequiresPermissions("system:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmSupplier scmSupplier)
    {
        startPage();
        SysUser sysUser = getSysUser();
        List<ScmSupplier> list = scmSupplierService.selectScmSupplierList(sysUser,scmSupplier);


        return getDataTable(list);
    }

    @GetMapping("/popupListPage")
    public String popupListPage( ModelMap mmap)
    {
        return prefix + "/supplierPop";
    }



    @PostMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("id") Long id)
    {

        SysUser sysUser = getSysUser();
        ScmSupplier scmSupplier = scmSupplierService.selectScmSupplierById(id);
        return AjaxResult.success(scmSupplier);

    }


    /**
     * 导出订单商品列表
     */
    @RequiresPermissions("system:supplier:export")
    @Log(title = "订单商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmSupplier scmSupplier)
    {
        SysUser sysUser = getSysUser();

        List<ScmSupplier> list = scmSupplierService.selectScmSupplierList(sysUser,scmSupplier);
        ExcelUtil<ScmSupplier> util = new ExcelUtil<ScmSupplier>(ScmSupplier.class);
        return util.exportExcel(list, "订单商品数据");
    }

    /**
     * 新增订单商品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单商品
     */
    @RequiresPermissions("system:supplier:add")
    @Log(title = "订单商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody ScmSupplierReq req)
    {
        SysUser sysUser = getSysUser();
        return toAjax(scmSupplierService.insertScmSupplier(req,sysUser));
    }

    /**
     * 修改订单商品
     */
    @RequiresPermissions("system:supplier:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmSupplier scmSupplier = scmSupplierService.selectScmSupplierById(id);
        mmap.put("scmSupplier", scmSupplier);
        return prefix + "/edit";
    }

    /**
     * 修改订单商品
     */
    @RequiresPermissions("system:supplier:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmSupplier scmSupplier = scmSupplierService.selectScmSupplierById(id);
        mmap.put("scmSupplier", scmSupplier);
        return prefix + "/detail";
    }

    /**
     * 修改保存订单商品
     */
    @RequiresPermissions("system:supplier:edit")
    @Log(title = "订单商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody ScmSupplierReq req)
    {
        SysUser sysUser = getSysUser();
        return toAjax(scmSupplierService.updateScmSupplier(req,sysUser));
    }

    /**
     * 删除订单商品
     */
    @RequiresPermissions("system:supplier:remove")
    @Log(title = "订单商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if(StringUtils.isEmpty(ids)){

            return toAjax(0);
        }
        String[] idList = ids.split(",");
        List<Long> idList1 = new ArrayList<>();
        for (String s : idList) {
            idList1.add(Long.valueOf(s));
        }
        return toAjax(scmSupplierService.deleteScmSupplierByIds(idList1));
    }


    /**
     * 查询供货商 商品信息
     */
    @PostMapping("/detail/list")
    @ResponseBody
    public TableDataInfo detailList(@RequestParam("id") Long id, ModelMap mmap)
    {
        if(id==null||id==0){
            return getDataTable(Lists.newArrayList());
        }
        //List<ScmPurchaseInventoryDetail> scmPurchaseInventoryDetails = scmPurchaseService.selectScmPurchaseInventoryDetailList(id);
        //List<ScmInventoryInDetail> list = scmInventoryInService.selectScmInventoryInDetailListList(id);
        SysUser sysUser = getSysUser();
        List<SupplierProjectResp> supplierProjectResps = scmSupplierService.selectScmSupplierProjectList(sysUser.getTenantId(), id);
        return getDataTable(supplierProjectResps);
    }
}
